package cn.caixiong.org.service.impl;

import cn.caixiong.basic.config.exception.BusinessException;
import cn.caixiong.basic.service.Impl.BaseServiceImpl;
import cn.caixiong.basic.util.PageList;

import cn.caixiong.basic.util.TreeUtil;
import cn.caixiong.org.domain.Department;
import cn.caixiong.org.domain.Employee;
import cn.caixiong.org.mapper.DepartmentMapper;
import cn.caixiong.org.query.DepartmentQuery;
import cn.caixiong.org.query.EmployeeQuery;
import cn.caixiong.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department, DepartmentQuery> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    //新增或修改
    @Override
    public void saveOrUpdate(Department department) {
        //1.代码细节,参数验证
        if (StringUtils.isBlank(department.getName())){
            throw new BusinessException("部门名称不能为空");
        }
        if (StringUtils.isBlank(department.getSn())){
            throw new BusinessException("部门编号不能为空");
        }
        if (department.getId() == null){
        departmentMapper.insertSelective(department);

    }
            make(department);
            departmentMapper.updateByPrimaryKeySelective(department);

    }
    //快捷ctrl+alt+m
    private void make(Department department) {
        Long[] parentIds = department.getParentIds();
        //这种是有上级的情况
        if (parentIds != null && parentIds.length > 0) {
            //获取是有上级的最后一位
            Long parentId = parentIds[parentIds.length - 1];
            //设置字段的值
            department.setParentId(parentId);
            //遍历
            StringBuilder dirPath = new StringBuilder("");
            for (Long id : parentIds) {
                dirPath.append("/").append(id);
            }
            dirPath.append("/").append(department.getId());
            //设置dirPath字段的值
            department.setDirPath(dirPath.toString());

        } else {
            department.setDirPath("/" + department.getId());
        }
    }


//分页查询
    @Override
    public PageList<Department> findPage(DepartmentQuery query) {
        //1.使用分页工具类
        PageHelper.startPage(query.getPage(),query.getSize());
        //2.执行查询
        List<Department> departments = departmentMapper.findPage(query);
        //3.封装结果
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        //4.封装我的自定义对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        return pageList;
    }

//高级查询
    @Override
    public List<Department> findParentDepts() {
        return departmentMapper.findParentDepts();
    }

    @Override
    public List<Department> treeDept() {
        //查询所有
        List<Department> departments = departmentMapper.selectAll();
        //使用工具类方法
        List<Department> tree = TreeUtil.getTree(departments);

        return tree;
    }
}
